using System;
using System.Data;
using System.Reflection;

namespace HSHLogicLayer
{
	/// <summary>
	/// Summary description for DBObject.
	/// </summary>
	public class DBObject
	{
		#region Delegates
		
		public delegate void dbobjectUpdated(DBObject sender, DBObject updatedSon);
		public event dbobjectUpdated DBObjectUpdated;	
		
		#endregion

		#region Fields

		protected DataRow m_drRawData;
        protected int m_PrimaryKey;

		#endregion

		#region Properties

		protected DataRow RawData
		{
			get { return m_drRawData; }
		}

		public int PrimaryKey
		{
            get { return m_PrimaryKey; }
		}

		public DateTime LastUpdate
		{
			get { return GetDateData("LAST_UPDATE"); } 
		}
		
		#endregion

		#region Constructors

		protected DBObject()
		{}	
		protected DBObject(int primaryKey)
		{
            m_PrimaryKey = primaryKey;
			Load(PrimaryKey);
		}
		
		protected DBObject(int primaryKey, DataRow RawDataRow)
		{
            m_PrimaryKey = primaryKey;
			LoadDataRow(RawDataRow);
		}

		#endregion	

		#region Data Get Methods

		protected int GetIntData(String Name)
		{
			return EmptyData.IntValue(RawData[Name]);
		}
		
		protected String GetStringData(String Name)
		{
			return EmptyData.StringValue(RawData[Name]);
		}

		protected bool GetBoolData(String Name)
		{
			return EmptyData.BoolValue(RawData[Name]);
		}

		protected DateTime GetDateData(String Name)
		{
			return EmptyData.DateValue(RawData[Name]);
		}

		protected Decimal GetDecimalData(String Name)
		{
			return EmptyData.DecimalValue(RawData[Name]);
		}

		protected char GetCharData(String Name)
		{
			return EmptyData.CharValue(RawData[Name]);
		}

		
		#endregion

		#region Load Methods

		protected void Load(int PrimaryKey)
		{
            throw new NotImplementedException();
/*
 * 
            DBFunctions objectFunctions = (DBFunctions)this.GetType().GetCustomAttributes(typeof(DBFunctions), true)[0];

			clsParameter[] arrParam = new clsParameter[1];
			arrParam[0] = new clsParameter(PrimaryKey, OracleTypes.Int32);

			DataTable rawDataTable = UniDBSingleton.Instance().UniDB.RunStoredFunctionReturnDataTable(objectFunctions.SelectFunction, ref arrParam);

			LoadDataRow(rawDataTable.Rows[0]);*/

		}

		protected void LoadDataRow(DataRow RawData)
		{			
			m_drRawData = RawData;
		}

		#endregion

		#region Refresh Methods
	
		public virtual void Refresh(DateTime DBLastUpdate)
		{
			if (DBLastUpdate > LastUpdate)
			{
				Load(PrimaryKey);
				PopEvent(null);
			}
		}
		public virtual void Refresh()
		{
/*			DBFunctions objectFunctions = (DBFunctions)this.GetType().GetCustomAttributes(typeof(DBFunctions), true)[0];

			clsParameter[] arrParam = new clsParameter[1];
			arrParam[0] = new clsParameter(PrimaryKey, OracleTypes.Int32);

			DateTime dtLastUpdate = UniDBSingleton.Instance().UniDB.RunStoredFunctionReturnDate(objectFunctions.LastUpdateFunction, ref arrParam);

			Refresh(dtLastUpdate);
 */
		}

		public virtual void Refresh(Type MarshaledType, int MarshaledId)
		{
			PopEvent(null);
		}
		#endregion

		#region Event Methods

		protected void PopEvent(DBObject updatedSon)
		{
			if (DBObjectUpdated != null) DBObjectUpdated(this, updatedSon);
		}

		#endregion

		#region TableMethods

		public DataTable GetTable()
		{
			DataTable dtNew = m_drRawData.Table.Clone();
			ConnectRow(ref dtNew);
			return dtNew;
		}

		public void ConnectRow(ref DataTable MainTable)
		{
			DataRow drNew = MainTable.NewRow();
			foreach (DataColumn currCol in MainTable.Columns)
				drNew[currCol.ColumnName] = m_drRawData[currCol.ColumnName];
			MainTable.Rows.Add(drNew);
			m_drRawData = drNew;
		}

		#endregion
	}

	public class DBFunctions : Attribute
	{
		protected String m_sSelectFunction;
		protected String m_sLastUpdateFunction;

		public DBFunctions(String SelectFunction, String LastUpdateFunction)

			: base()
		{
			m_sSelectFunction		= SelectFunction;
			m_sLastUpdateFunction	= LastUpdateFunction;
		}

		public String SelectFunction
		{
			get { return m_sSelectFunction; }
			set { m_sSelectFunction = value; }
		}

		public String LastUpdateFunction
		{
			get { return m_sLastUpdateFunction; }
			set { m_sLastUpdateFunction = value; }
		}
	}	
	
}
